package com.yj.springbootmybatis.service.center;

import com.yj.springbootmybatis.mapper.UserMapper;
import com.yj.springbootmybatis.pojo.User;
import com.yj.springbootmybatis.pojo.center.UserInfoBo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class CenterServiceImpl implements CenterService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User queryUserInfo(String userId) {

        User user = new User();
        user.setId(userId);
        user = userMapper.selectOne(user);
        user.setPassword(null);
        return user;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public User updateUserInfoById(String userId, UserInfoBo userInfoBo) {

        User user = new User();
        BeanUtils.copyProperties(userInfoBo,user);
        user.setId(userId);
        user.setUpdateTime(new Date());
        userMapper.updateByPrimaryKeySelective(user);

        user = queryUserInfo(userId);

        return user;
    }

    @Override
    public User updateUserPhoneById(String userId, String faceUrl) {

        User updateUser = new User();
        updateUser.setId(userId);
        updateUser.setFace(faceUrl);
        updateUser.setUpdateTime(new Date());

        userMapper.updateByPrimaryKeySelective(updateUser);

        User user = queryUserInfo(userId);

        return user;
    }


}
